package client.gui;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.util.*;



import messages.*;



import sharedspace.*;



/**
 * This class is a panel that responsible to show all the teacher options
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */


public class GUI_Teacher extends GUI_AbstractMainMenu {
	private static final long serialVersionUID = 1L;
    private User user = null;
	private JButton addNewQuestion = null;
	private JButton createTest=null;
	private JButton approvingGrades = null;
	private JButton delQBtn = null;
	private JButton editQBtn = null;
	private JButton exctBtn=null;
	private JButton testLock=null;
	private JButton extraTimeBtn = null;
	private JButton ManualTestBtn=null;
	private JButton statisticBtn = null;
	private JButton GradesBtn = null;
	
	/**
	 * This is the default constructor
	 * @param MS
	 */
	public GUI_Teacher(Client_GUI ms, User user) {
		super(MainMenuType.TEACHER,ms);
		this.ms=ms;
		initiateTeacher();
		this.user=user;
		greetMsg.setText("Welcome "+ms.lg.getCurrentUsername()+"!");
		mainMenu.setText("Teacher");

	}

	/**
	 * This method initializes the Login screen
	 */
	private void initiateTeacher() {
			
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getAddNewQuestion(), null);
		background.add(getCreateTest(), null);
		background.add(getApprovingGrade(),null);
		//background.add(getDeleteQuestionBtn(),null);
		background.add(getEditQuestionBtn(),null);
		background.add(getExecuteTest(),null);
		background.add(getTestLock(),null);
		background.add(getExtraTimeBtn(),null);
		background.add(getManualTestBtn(),null);
		background.add(getViewStatisticReport());
		background.add(getViewGrades());
	}
	private JButton getAddNewQuestion()
	{
		if (addNewQuestion == null)
		{
			//set graphics
			addNewQuestion = new JButton();
			addNewQuestion.setFont(new Font("Arial", Font.PLAIN, 16));
			addNewQuestion.setBounds(new Rectangle(53, 240, 240, 34));
			addNewQuestion.setText("Add New Question");
			this.addNewQuestion.addActionListener(new java.awt.event.ActionListener() {
				
			

				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						ms.taq =(new GUI_Teacher_Add_Question(ms,user)); 
						ms.setContentPane(ms.taq);
						ms.taq.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});
									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
						}
				}
			}
			);
		}
		return this.addNewQuestion;
	}
	
	private JButton getCreateTest(){
		if (createTest == null)
		{
			//set graphics
			createTest  = new JButton();
			createTest .setFont(new Font("Arial", Font.PLAIN, 16));
			createTest .setBounds(new Rectangle(313, 240, 240, 34));
			createTest .setText("Create Test");
			this.createTest .addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.cst=new GUI_SelectSubjectTest(ms,user);
						ms.setContentPane(ms.cst);
						//ms.ct=(new GUI_CreateTest(ms, user));
						
						ms.cst.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});}
		
		
					return this.createTest;
		
	}
	
	private JButton getApprovingGrade()
	{
		if (this.approvingGrades == null)
		{
			approvingGrades = new JButton("Approving Grades");
			approvingGrades .setFont(new Font("Arial", Font.PLAIN, 16));
			approvingGrades .setBounds(new Rectangle(570, 240, 240, 34));
			
			approvingGrades.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.tag=new GUI_TeacherApprovingGrade(ms, user);
						if (ms.tag.b==true)
						{	
							ms.setContentPane(ms.tag);
							ms.tag.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

						}			
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return approvingGrades;
	}

	private JButton getViewGrades()
	{
		if (this.GradesBtn == null)
		{
			GradesBtn = new JButton("View Grades");
			GradesBtn .setFont(new Font("Arial", Font.PLAIN, 16));
			GradesBtn .setBounds(new Rectangle(570, 360, 240, 34));
			
			GradesBtn.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try{	 
						ms.tvg=new GUI_Teacher_ViewGrades(ms);
							ms.setContentPane(ms.tvg);
							ms.tvg.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});
			
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return GradesBtn;
	}
	
	private JButton getViewStatisticReport()
	{
		if (this.statisticBtn == null)
		{
			statisticBtn = new JButton("View Statistics");
			statisticBtn .setFont(new Font("Arial", Font.PLAIN, 16));
			statisticBtn .setBounds(new Rectangle(570, 300, 240, 34));
			
			statisticBtn.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try{	 
						ms.tvs=new GUI_Teacher_View_Statistics(ms);
							ms.setContentPane(ms.tvs);
							ms.tvs.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});
			
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return statisticBtn;
	}
	
	private JButton getDeleteQuestionBtn()
	{
		if (this.delQBtn == null)
		{
			delQBtn = new JButton("Delete Question");
			delQBtn .setFont(new Font("Arial", Font.PLAIN, 16));
			delQBtn .setBounds(new Rectangle(53, 360, 240, 34));
			
			delQBtn.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.tdq=new GUI_Teacher_Delete_Question(ms);
						ms.setContentPane(ms.tdq);
						//ms.ct=(new GUI_CreateTest(ms, user));
						
						ms.tdq.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return delQBtn;
	}
	
	private JButton getEditQuestionBtn()
	{
		if (this.editQBtn == null)
		{
			editQBtn = new JButton("Edit Question");
			editQBtn .setFont(new Font("Arial", Font.PLAIN, 16));
			editQBtn .setBounds(new Rectangle(53, 300, 240, 34));
			editQBtn.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.teq=new GUI_Teacher_Edit_Question(ms);
						ms.setContentPane(ms.teq);
						//ms.ct=(new GUI_CreateTest(ms, user));
						
						ms.teq.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return editQBtn;
	}
	
	private JButton getExecuteTest()
	{
		if (this.exctBtn == null)
		{
			//set graphics
			exctBtn = new JButton("Execute Exam");
			exctBtn.setFont(new Font("Arial", Font.PLAIN, 16));
			exctBtn.setBounds(new Rectangle(313, 360, 240, 34));
			this.exctBtn.addActionListener(new java.awt.event.ActionListener() {
				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.texm=new GUI_Teacher_Execute_Exam (ms,user);
						ms.setContentPane(ms.texm);
						
						
						ms.texm.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return exctBtn;
	}//end  getExecuteTest
	
	private JButton getExtraTimeBtn()
	{
		if (this.extraTimeBtn == null)
		{
			//set graphics
			extraTimeBtn = new JButton("Request Test Extention");
			extraTimeBtn.setFont(new Font("Arial", Font.PLAIN, 16));
			extraTimeBtn.setBounds(new Rectangle(313, 440, 240, 34));
			this.extraTimeBtn.addActionListener(new java.awt.event.ActionListener() {
				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.ter=new GUI_Teacher_Extension_Request(ms);
						ms.setContentPane(ms.ter);
						
						
						ms.ter.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});
		}
		
		return extraTimeBtn;
	}//end  getExecuteTest
	
	public JButton getTestLock()
	{
		if (testLock == null)
		{
			testLock = new JButton("Test Lock");
			testLock.setFont(new Font("Arial", Font.PLAIN, 16));
			testLock.setBounds(new Rectangle(313, 600, 240, 34));
			testLock.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					try
					{
						MessageTeacherExecuteTest msgtet = new MessageTeacherExecuteTest(user.getUserID());
						ms.client.sendToServer(msgtet);
						MessageTeacherExecuteTestReply msgtetrep = (MessageTeacherExecuteTestReply)ms.client.GetMessage();
						List<ExecuteTest> eList = msgtetrep.geteList();
						if (eList.size() == 0)
						{
							JOptionPane.showMessageDialog(null, "You don't have test to lock!");
							return;
						}
						Object[] possibilities = new Object[eList.size()];
						for (int i = 0; i<eList.size(); i++)
							if (eList.get(i).getTest() != null)
								possibilities[i] = (i+1)+".Test: "+ eList.get(i).getExecuteTestID()
												+ "; TestID: "+ eList.get(i).getTest().getTestID()+" in course: "+ eList.get(i).getTest().getCourse().getCourseName();
							else possibilities[i] = (i+1)+".Test: "+ eList.get(i).getExecuteTestID()
													+ "; Manula Test in "+ eList.get(i).getWordTestPath();
						
						String result = (String)JOptionPane.showInputDialog(null, "Choose test to Lock", "Test Lock", JOptionPane.PLAIN_MESSAGE, null, possibilities, possibilities[0]);
						if (result != null)
						{
							int option = JOptionPane.showConfirmDialog(null, "Are you sure you\n want to lock the test?", "Test Lock", JOptionPane.YES_NO_OPTION);
							if (option == JOptionPane.YES_OPTION)
							{
								result = result.substring(0, 1);
								int i = Integer.parseInt(result);
								i--;
								ExecuteTest e = eList.get(i);
								MessageLockTest msglt = new MessageLockTest(e.getExecuteTestID());
								ms.client.sendToServer(msglt);
							}
						}
					}
					catch(IOException e)
					{
						e.printStackTrace();
					}
					
				}
			});
		}
		return testLock;
	}
	
	public JButton getManualTestBtn(){
		if (ManualTestBtn == null)
		{
			//set graphics
			ManualTestBtn  = new JButton();
			ManualTestBtn .setFont(new Font("Arial", Font.PLAIN, 16));
			ManualTestBtn .setBounds(new Rectangle(313, 300, 240, 34));
			ManualTestBtn .setText("Create Manual Test");
			this.ManualTestBtn .addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						 
						ms.cmt=new GUI_Create_Manual_Test(ms,user);
						ms.setContentPane(ms.cmt);
						
						ms.cmt.btnBack.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								System.out.println("actionPerformed");
								ms.setContentPane(ms.tchr);
							}});

									
						}
					catch (Exception e1){
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
			});}
		
		
					return this.ManualTestBtn;
		
	}
		
	
	public User getTeachetUser(){
		return this.user;
	}
	
	public String getTeacherID(){
		return user.getUserID();
	}
}  
